module.exports = function ($scope, $resource, $modal, toaster) {

	//查询法规列表
	$scope.search = function() {
		$resource('/api/as/atc/policy/getPolicyList', {}, {}).save({}, function(res){
            if (res.errcode === 0) {
                $scope.policyList = res.data;
            } else {
                toaster.error({title: '', body: '查询政策及法规信息失败'});
            }
        });
	}

	$scope.searchform = {};
	$scope.search();


    //修改发布状态
    $scope.updateStatus = function(item) {
		var msg, para = {'code': item.code}
		if(item.status == '1'){
			msg = '撤销后, 将停止招徕奖励补贴 \r\n 确认撤销本条补贴规则吗?';
			para.status = '0'
		} else {
			msg = '发布后, 所有企业都按照本条补贴规则领取补贴.';
			para.status = '1'
		}
        if(confirm(msg)){
			$resource('/api/ac/atc/policyService/updatePolicyStatus', {}, {}).save(para, function(res){
				if (res.errcode === 0) {
					$scope.search();
				} else {
					toaster.error({title: '', body: res.errmsg});
				}
			});
        }
	};

	//开启或停用
    $scope.changeStatus = function(item) {
		var msg, para = {'code': item.code}
		if(item.stop_status == '0'){
			msg = '停用后，将不再计算招徕奖励补贴，但不会撤销本条补贴规则吗？';
			para.stop_status = '1'
		} else {
			msg = '开启后，继续计算招徕奖励补贴。';
			para.stop_status = '0'
		}
		if(confirm(msg)){
			$resource('/api/as/atc/policy/setStopStatus', {}, {}).save(para, function(res){
				if (res.errcode === 0) {
					item.stop_status = para.stop_status;
					toaster.success({title: '', body: '修改成功'});
				} else {
					toaster.error({title: '', body: '修改失败'});
				}
			});
        }
	}
	
	//添加
	$scope.addPolicy = function() {
		var item = {
			'range_limit': '0'
		}
		var modalInstance = $modal.open({
			template: require('../views/eidtPolicy.html'),
			controller: 'eidtPolicy',
			size: 'lg',
			resolve: {
				item: function () {
					return item;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			init();
		});
	}

    //修改
	$scope.edit = function(item) {
		var modalInstance = $modal.open({
			template: require('../views/eidtPolicy.html'),
			controller: 'eidtPolicy',
			size: 'lg',
			resolve: {
				item: function () {
					return item;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			init();
		});
    };

	//删除
	$scope.delete = function(code, index) {
		if(confirm('确认删除本条补贴规则吗?')){
			$resource('/api/as/atc/policy/deletePolicy', {}, {}).save({'code': code}, function(res){
				if (res.errcode === 0) {
					toaster.success({title: '', body: '删除成功'});
					$scope.policyList.splice(index, 1);
				} else {
					toaster.error({title: '', body: '删除失败, 请稍后再试'});
				}
			});
		}
	}

};